package com.jianzong.lingxuan.api.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jianzong.lingxuan.api.mapper.SectUserMapper;
import com.jianzong.lingxuan.api.service.ApiService;
import com.jianzong.lingxuan.base.Result;
import com.jianzong.lingxuan.common.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


/**
 * @auth Leiwenlong
 * @since 2021/7/19
 *  测试页面跳转
 */
@Controller
public class IndexController {

    final SectUserMapper sectUserMapper;
    final RedisService redisService;

    public IndexController(SectUserMapper sectUserMapper, RedisService redisService){
        this.sectUserMapper = sectUserMapper;
        this.redisService = redisService;
    }

    @GetMapping("/login")
    public String login(){
       return "login";
    }

    @GetMapping("/admin")
    public String admin(){
        return "admin";
    }

    @GetMapping("/newLogin")
    public String newLogin(){
        return "newLogin";
    }

    @GetMapping("/chat")
    public String chat(){
        return "chat";
    }

    @GetMapping("/user/{id}")
    @CrossOrigin
    public String user(@PathVariable String id ,Model model) throws JsonProcessingException {
        Map<String, Object> map = sectUserMapper.selectUserInfoByAdmin(id);
        ObjectMapper objectMapper = new ObjectMapper();
        String user = objectMapper.writeValueAsString(map);

        //登录token
        String token = IdUtil.simpleUUID();
        redisService.set(token,map);

        model.addAttribute("token", token);
        model.addAttribute("user", user);
        return "user";
    }
}
